<?php
	class Db_user extends CI_Model{
		public function __construct(){
			parent::__construct();
		}
		
		public function user_all(){
			$config['base_url'] = base_url().'manage_user/user_list';
			$config['total_rows'] = $this->db->count_all("mobile_user");
			$config['per_page'] = 20;
			$config['full_tag_open'] = '<div class="pages">';
			$config['full_tag_close'] = '<div>';
			$this->pagination->initialize($config);
			$this->db->select('t1.name as type,t2.pkid,t2.name,t2.email');
			$this->db->from("mobile_user_access t1");
			$this->db->join("mobile_user t2", 't1.pkid = t2.role_pkid');
			$this->db->limit($config['per_page'],$this->uri->segment(3));
			$this->db->order_by("t2.pkid", "desc");
			$res = $this->db->get();
			//==
			$no = $this->uri->segment(3)+1;
			$i = 1;
			$arr = $res->result_array();
			$cat = array();
			foreach ($arr as $P){
				if($i %2 == 0)
					$tmp['back']							="#FFFFFF";
				else
					$tmp['back']							="#F4F4F4";
				$i++;
				$tmp['no']							=$no;
				$tmp['pkid']						=$P['pkid'];
				$tmp['name']						=$P['name'];
				$tmp['type']						=$P['type'];
				$tmp['email']						=$P['email'];
				$cat[] = $tmp;
				$no = $no+1;
			}
			$num_row = $config['total_rows'];
			$pagelink  = $this->pagination->create_links();
			return array($cat,$num_row,$pagelink,$config['total_rows']);
		}
		
		public function department($group=""){
			$this->db->order_by("pkid","desc");
			$res = $this->db->get('hotel_department');
			$str = '<option value="">=select department=</option>';
			foreach ($res->result_array() as $P){
				if($group == $P['pkid']){
					$check = 'selected="selected"';
				}else{
					$check = "";
				}
				$str .='<option value="'.$P['pkid'].'" '.$check.'>'.$P['name_en'].'</option>';
			}
			return $str;
		}
		
		public function access_all(){
			$config['base_url'] = base_url().'manage_user/user_list';
			$config['total_rows'] = $this->db->count_all("mobile_user_access");
			$config['per_page'] = 20;
			$config['full_tag_open'] = '<div class="pages">';
			$config['full_tag_close'] = '<div>';
			$this->pagination->initialize($config);
			$this->db->select('t1.name as dname,t2.pkid,t2.name');
			$this->db->from("mobile_user t1");
			$this->db->join("mobile_user_access t2", 't1.pkid = t2.department_pkid');
			$this->db->limit($config['per_page'],$this->uri->segment(3));
			$this->db->order_by("t2.pkid", "desc");
			$res = $this->db->get();
			//==
			$no = $this->uri->segment(3)+1;
			$i = 1;
			$arr = $res->result_array();
			$cat = array();
			foreach ($arr as $P){
				if($i %2 == 0)
					$tmp['back']							="#FFFFFF";
				else
					$tmp['back']							="#F4F4F4";
				$i++;
				$tmp['no']							=$no;
				$tmp['pkid']						=$P['pkid'];
				$tmp['name']						=$P['name'];
				$tmp['dname']					=$P['dname'];
				$cat[] = $tmp;
				$no = $no+1;
			}
			$num_row = $config['total_rows'];
			$pagelink  = $this->pagination->create_links();
			return array($cat,$num_row,$pagelink,$config['total_rows']);
		}
		
		public function country_list($id=""){
			$this->db->order_by("name","asc");
			$res =$this->db->get("mobile_country");
			$str = '<option value="">=select country=</option>';
			foreach ($res->result_array() as $P){
				if($id == $P['pkid']) $ch = 'selected="selected"';
				else $ch="";
				$str .='<option value="'.$P['pkid'].'" '.$ch.'>'.$P['name'].'</option>';
			}
			return $str;
		}
		
		public function access_list($id=""){
			$this->db->order_by("pkid","asc");
			$res =$this->db->get("mobile_user_access");
			$str = '<option value="">=select level=</option>';
			foreach ($res->result_array() as $P){
				if($id == $P['pkid']) $ch = 'selected="selected"';
				else $ch="";
				$str .='<option value="'.$P['pkid'].'" '.$ch.'>'.$P['name'].'</option>';
			}
			return $str;
		}
		
	}
?>